package cn.vshare.service.impl;

import cn.vshare.dao.CoinDao;
import cn.vshare.dao.TradeDao;
import cn.vshare.entity.Coin;
import cn.vshare.entity.Trade;
import cn.vshare.entity.User;
import cn.vshare.entity.Video;
import cn.vshare.service.CoinService;
import cn.vshare.util.CoinChageException;
import cn.vshare.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
public class CoinServiceImpl implements CoinService {
    @Autowired
    private CoinDao coinDao;
    @Autowired
    private TradeDao tradeDao;


    @Override
    public void openUserAccount(String userId) {
        String cid= UUID.randomUUID().toString();
        User user=new User();
        user.setId(userId);
        Coin coin=new Coin();
        coin.setTotal(0);
        coin.setUser(user);
        coin.setId(cid);
        coinDao.addUserCoin(coin);

        Trade trade=new Trade();
        String tid=UUID.randomUUID().toString();
        trade.setId(tid);
        trade.setUser(user);
        trade.setSpend(0);
        trade.setCause("开通账户");
        trade.setTradeDate(DateUtil.getCurrDate());
        trade.setBalance(0);
        tradeDao.addTrade(trade);

    }

    @Override
    public Coin recharge(String userId, int count) {
        Coin coin=coinDao.queryByUserId(userId);
        int total=coin.getTotal()+count;
        coin.setTotal(total);
        coinDao.updateUserCoin(coin);

        Trade trade=new Trade();
        String tid=UUID.randomUUID().toString();
        trade.setId(tid);
        User user=new User();
        user.setId(userId);
        trade.setUser(user);
        trade.setSpend(count);
        trade.setCause("充值");
        trade.setTradeDate(DateUtil.getCurrDate());
        trade.setBalance(total);
        tradeDao.addTrade(trade);

        return coin;
    }

    @Override
    public Coin consume(String userId, int count) {
        Coin coin=coinDao.queryByUserId(userId);
        int total=coin.getTotal();
        coin.setTotal(total-count);
        coinDao.updateUserCoin(coin);

        Trade trade=new Trade();
        String tid=UUID.randomUUID().toString();
        trade.setId(tid);
        User user=new User();
        user.setId(userId);
        trade.setUser(user);
        trade.setSpend(-count);
        trade.setCause("消费");
        trade.setTradeDate(DateUtil.getCurrDate());
        trade.setBalance(total);
        tradeDao.addTrade(trade);

        return coin;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = CoinChageException.class)
    @Override
    public Coin rewardToUser(String myId, String userId, int count,String videoId) throws CoinChageException {
        Coin mycoin=coinDao.queryByUserId(myId);
        if (mycoin.getTotal()<count){
            throw new CoinChageException("你的金币不足请充值");
        }
        mycoin.setTotal(mycoin.getTotal()-count);
        coinDao.updateUserCoin(mycoin);

        Video video=new Video();
        video.setId(videoId);

        Trade trade=new Trade();
        String tid=UUID.randomUUID().toString();
        trade.setId(tid);
        User user=new User();
        user.setId(myId);
        trade.setUser(user);
        trade.setSpend(-count);
        trade.setCause("打赏");
        trade.setVideo(video);
        trade.setTradeDate(DateUtil.getCurrDate());
        trade.setBalance(mycoin.getTotal());
        tradeDao.addTrade(trade);
        Coin userCoin=coinDao.queryByUserId(userId);
        userCoin.setTotal(userCoin.getTotal()+count);
        coinDao.updateUserCoin(userCoin);

        Trade trade1=new Trade();
        String tid1=UUID.randomUUID().toString();
        trade1.setId(tid1);
        User user1=new User();
        user1.setId(userId);
        trade1.setUser(user1);
        trade1.setSpend(count);
        trade1.setVideo(video);
        trade1.setCause("获取赏金");
        trade1.setTradeDate(DateUtil.getCurrDate());
        trade1.setBalance(userCoin.getTotal());
        tradeDao.addTrade(trade1);
        return mycoin;
    }

    @Override
    public Coin queryCoinMess(String myId) {
        return coinDao.queryByUserId(myId);
    }

    @Override
    public int querySumCoinByVideoId(String videoId) {
        return coinDao.querySumCoinByVideoId(videoId);
    }
}
